<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/11/21
 * Time: 10:00
 */

namespace app\index\controller;


use Protocol\Curl;
use think\Controller;
use think\facade\Log;

class ThirdParty extends Controller
{
    /**
     * @param $filePath
     * @param $cardtype
     * @return array
     * @throws \BaseException
     * @content 聚合数据---图片识别
     */
    static function certificates($filePath, $cardtype)
    {
        $res = self::upload(self::makePostData($filePath, $cardtype));
        if (is_string($res)) {
            $res = json_decode($res, true);
        }
        if ($res['error_code'] != 0) {
            return array('status' => false, 'msg' => $res['reason']);
        }
        if ($cardtype == 2007) {
            return array('status' => true, 'vin' => $res['vin']);
        } else {
            return array('status' => true, 'carNum' => $res['result']['车牌号']);
        }
    }

    /**
     * @param $filePath
     * @param $cardtype
     * @return array
     * @content 聚合数据-图片识别-整理参数
     */
    static function makePostData($filePath, $cardtype)
    {
        $cFile = new \CURLFile($filePath);
        return [
            'key' => '91b22f09248013ad66110134b7617977',
            'cardType' => $cardtype,//2表示身份证正面,一定要看清楚你的图片到底是什么类型的图片
            'pic' => $cFile,
            'userid' => date("YmdHis", time())
        ];
    }

    /**
     * @param $postData
     * @return mixed
     * @throws \BaseException
     * @content 聚合数据--图片识别--接口
     */
    static function upload($postData)
    {
        $httpInfo = [];
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://v.juhe.cn/certificates/query.php');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //如果成功,只将结果返回,不自动输出任何内容,如果失败返回false
        //设置为post请求类型
        curl_setopt($ch, CURLOPT_POST, 1);
        //设置具体的post数据
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        $response = curl_exec($ch);
        if ($response === false) {
            //如果提示你上传图片太小等,可以打印出$cFile这个变量看看具体图片信息
            //一般因为路径引起的错误都会提示:couldn't open file "cc.jpg"
            throw new \BaseException(['msg' => curl_error($ch), 'status' => false, 'debug' => false]);
        }
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($httpCode != 200) {
//            echo '服务器状态码:' . $httpCode;
            throw new \BaseException(['msg' => $httpCode, 'status' => false, 'debug' => false]);
        }
        $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
        curl_close($ch);
        return $response;
    }

    /**
     * @param string $touser 用户的openid
     * @param string $template_id 模板id
     * @param array $data 模板消息的内容(二维数组)
     * @param string $url 跳转地址(自动拼接)
     * 例如 :  array(
     * 'first' => array('value' => "值", 'color' => '#000000'),
     * 'keyword1' => array('value' => "值", 'color' => '#000000'),
     * 'keyword2' => array('value' => "值", 'color' => '#000000'),
     * 'keyword3' => array('value' => "值", 'color' => '#000000'),
     * 'keyword4' => array('value' => "值", 'color' => '#000000'),
     * 'remark' => array('value' => "值", 'color' => '#000000'),
     * );
     * @content 发送模板消息 (多文字时,使用"\n" 进行换行显示)
     * @return bool
     */
    static function sendTemplate($touser, $template_id, $url, $data)
    {
        if (empty($touser)) {
            return true;
        } else {
            # 获取token
            $token = getAccess_token();
            $_curl = new Curl();
            # 模板消息接口地址
            $_url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $token;
            # 拼接模板数据
            $data_msg = json_encode(array(
                'touser' => $touser,
                'template_id' => $template_id,
                'url' => $url,
                'data' => $data
            ));
            # 发送
            $_receipt = $_curl->post($_url, $data_msg, 'json');
            # 对返回结果进行处理(json 转 array)
            $_receipt = json_decode($_receipt, true);
//            Log::record("模板消息->用户:" . $touser . "返回结果".json_encode($_receipt));
            return $_receipt;
//            errorLogs("模板消息->用户:" . $touser . "返回结果".json_encode($_receipt));

            if ($_receipt['errcode'] == 0 and $_receipt['errmsg'] == 'ok') {
                return true;
            } else {
                return true;
            }
        }
    }
}
